package com.jwell.panrank.ui.activity

import android.os.Bundle
import com.jwell.panrank.R
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.Toast
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.driverapp.ui.base.BaseActivity
import com.jwell.panrank.contract.MainContract
import com.jwell.panrank.presenter.MainPresenter
import com.jwell.panrank.tools.ActivityManager
import com.jwell.panrank.tools.DataModel
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : MVPBaseActivity<MainPresenter>(), MainContract.View, View.OnClickListener {


    override fun createPresenter(): MainPresenter? {
        return MainPresenter()
    }

    private val REQUEST_CODE = 101
    private var permisson: List<String>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ActivityManager.getInstance().pushOneActivity(this)
        setTransportStatusBar()
        ll_codeScan.setOnClickListener(this)
        rl_1.setOnClickListener(this)
        rl_2.setOnClickListener(this)
        rl_4.setOnClickListener(this)
        rl_5.setOnClickListener(this)
        rl_6.setOnClickListener(this)
        text_exit.setOnClickListener(this)
        DataModel.getInstance()?.userName?.let {
            text_userName.text = "欢迎您," + it
        }
        setPermission()

    }

    override fun onResume() {
        super.onResume()
        presenter?.checkPermission()
        try {
            presenter?.getUnloadingPoint()
        } catch (e: Exception) {

        }
        try {
            presenter?.getMeasurePlaceList()
        } catch (e: Exception) {

        }
    }

    override fun setPermission() {
        DataModel.getInstance()?.permission?.let {
            permisson = it
        }
        permisson?.let {
            if (it.contains("Pages_Business_PreRegistration")) {//预登记权限
                rl_22.visibility = View.VISIBLE
            } else {
                rl_22.visibility = View.GONE
            }
            if (it.contains("Pages_Business_PreRegistration_PreRegistrationManagement_Create")) {//收料计划权限
                rl_11.visibility = View.VISIBLE
            } else {
                rl_11.visibility = View.GONE
            }
            if (it.contains("Pages_BusinessQuery_ElectronicList")) {//电子磅单查询权限
                rl_44.visibility = View.VISIBLE
            } else {
                rl_44.visibility = View.GONE
            }
            if (it.contains("Pages_Business_Waybill")) {//运单管理权限
                rl_55.visibility = View.VISIBLE
            } else {
                rl_55.visibility = View.GONE
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.text_exit -> {
//                toast("monkey测试，关闭退出登录功能")
                skipToLogin(true)
            }
            R.id.ll_codeScan -> {
                val intent = Intent(this, CaptureActivity::class.java)
                startActivityForResult(intent, REQUEST_CODE)
            }
            R.id.rl_1 -> {//收料计划
                val intent = Intent(this, PreRegisterPlaneActivity::class.java)
                startActivity(intent)
            }
            R.id.rl_2 -> {//预登记
                val intent = Intent(this, PreRegisterActivity::class.java)
                startActivity(intent)
            }
            R.id.rl_4 -> {//电子磅单查询
                val intent = Intent(this, ElectronicPoundActivity::class.java)
                startActivity(intent)
            }
            R.id.rl_5 -> {//运单管理
                val intent = Intent(this, WaybillActivity::class.java)
                startActivity(intent)
            }
            R.id.rl_6 -> {//进厂车数查询
                val intent = Intent(this, CarInquireActivityy::class.java)
                startActivity(intent)
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        // 过滤按键动作，按返回键不退出程序，直接退到桌面
        if (event!!.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true)//true对任何Activity都适用
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onBackPressed() {
        moveTaskToBack(true)
    }
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (resultCode == CaptureActivity.RESULT_CODE_QR_SCAN) { //RESULT_OK = -1
//            val bundle = data?.extras
//            val scanResult = bundle?.getString("qr_scan_result")
//            Toast.makeText(this, scanResult, Toast.LENGTH_LONG).show()
//        }
//    }
}
